<template>
  <el-dialog
    :title="!dataForm.contractId ? '新增' : (dataForm.startTime !=='' ? '修改' : '续约')"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="干系人" prop="stakeholder">
        <!-- <el-input v-model="dataForm.stakeholder" placeholder="干系人"></el-input> -->
        <el-select v-model="dataForm.stakeholder" placeholder="请选择" class="width400" :disabled="disabledBox">
          <el-option
            v-for="item in optionsStakeholder"
            :key="item.crewId"
            :label="item.crewName"
            :value="item.crewName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="合同编号" prop="contractNum">
        <el-input v-model="dataForm.contractNum" placeholder="合同编号" class="width400"></el-input>
      </el-form-item>
      <el-form-item label="合同类型" prop="contractType">
        <!-- <el-input v-model="dataForm.contractType" placeholder="合同类型"></el-input> -->
        <el-select v-model="dataForm.contractType" placeholder="请选择" class="width400">
          <el-option
            v-for="item in optionsContractType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input v-model="dataForm.contractName" placeholder="合同名称" class="width400"></el-input>
      </el-form-item>
      <el-form-item label="起始时间" prop="startTime">
        <!-- <el-input v-model="dataForm.startTime" placeholder="起始时间"></el-input> -->
        <el-date-picker
          v-model="dataForm.startTime"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="截止时间" prop="endTime">
        <!-- <el-input v-model="dataForm.endTime" placeholder="截止时间"></el-input> -->
        <el-date-picker
          v-model="dataForm.endTime"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="合同状态" prop="contractStatus">
        <!-- <el-input v-model="dataForm.contractStatus" placeholder="合同状态"></el-input> -->
        <el-select v-model="dataForm.contractStatus" placeholder="请选择" class="width400">
          <el-option
            v-for="item in optionsContractStatus"
            :key="item.value"
            :label="item.value"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="委托方（甲方）" prop="partyA">
        <el-input v-model="dataForm.partyA" placeholder="委托方（甲方）" class="width400"></el-input>
      </el-form-item>
      <el-form-item label="受托方（乙方）" prop="partyB">
        <el-input v-model="dataForm.partyB" placeholder="受托方（乙方）" class="width400"></el-input>
      </el-form-item>
      <el-form-item label="签订日期" prop="signData">
        <!-- <el-input v-model="dataForm.signData" placeholder="签订日期"></el-input> -->
        <el-date-picker
          v-model="dataForm.signData"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="创建人" prop="createUserId">
        <el-input v-model="dataForm.createUserId" placeholder="创建人"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="所属公司" prop="affiliatedCompanyId">
        <el-select v-model="dataForm.affiliatedCompanyId" placeholder="请选择">
          <el-option
            v-for="item in optionsAffiliatedCompanyId"
            :key="item.affiliatedCompanyId"
            :label="item.companyName"
            :value="item.affiliatedCompanyId"
          ></el-option>
        </el-select>
      </el-form-item>-->
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        contractId: 0,
        stakeholder: "",
        contractNum: "",
        contractType: "",
        contractName: "",
        startTime: "",
        endTime: "",
        contractStatus: "",
        partyA: "",
        partyB: "",
        signData: "",
        createUserId: "",
        affiliatedCompanyId: "",
        createTime: ""
      },
      dataRule: {
        // stakeholder: [
        //   { required: true, message: "合同干系人不能为空", trigger: "blur" }
        // ],
        // contractNum: [
        //   { required: true, message: "合同编号不能为空", trigger: "blur" }
        // ],
        contractType: [
          { required: true, message: "合同类型不能为空", trigger: "blur" }
        ],
        contractName: [
          { required: true, message: "合同名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "起始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "截止时间不能为空", trigger: "blur" }
        ],
        contractStatus: [
          { required: true, message: "合同状态不能为空", trigger: "blur" }
        ],
        partyA: [
          { required: true, message: "委托方（甲方）不能为空", trigger: "blur" }
        ],
        partyB: [
          { required: true, message: "受托方（乙方）不能为空", trigger: "blur" }
        ]
        // signData: [
        //   { required: true, message: "签订日期不能为空", trigger: "blur" }
        // ],
        // createUserId: [
        //   { required: true, message: "创建人不能为空", trigger: "blur" }
        // ],
        // affiliatedCompanyId: [
        //   { required: true, message: "所属公司不能为空", trigger: "blur" }
        // ],
        // createTime: [
        //   { required: true, message: "创建时间不能为空", trigger: "blur" }
        // ]
      },
      optionsStakeholder: [],
      optionsContractType: [],
      optionsContractStatus: [],
      optionsAffiliatedCompanyId: [],
      disabledBox: false,
      info: 1,
    };
  },
  methods: {
    init(id, iStatus, info) {
      console.log('------------id',id)
      console.log('------------info',info)
      this.info = info
      if (info !== 0 && info !== 1 ) {
        this.disabledBox = true
      } else {
        if (id) {
          this.disabledBox = true
        } else {
          this.disabledBox = false
        }
      }
      this.$http({
        url: this.$http.adornUrl("/generator/crewinfo/select"),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.optionsStakeholder = data && data.code === 0 ? data.list : [];
        if (info !== 0) {
          var _this = this
          data.list.forEach(function(value, index, array) {
            if (value.crewId === info) {
              _this.dataForm.stakeholder = value.crewName
            }
          })
        }
      });
      
      this.$http({
        url: this.$http.adornUrl("/generator/affiliatedcompanyinfo/select"),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.optionsAffiliatedCompanyId =
          data && data.code === 0 ? data.list : [];
      });

      this.$http({
        url: this.$http.adornUrl(`/generator/datadictionary/info/${21}`),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.optionsContractType = data && data.code === 0 ? data.list : [];
      });

      this.$http({
        url: this.$http.adornUrl(`/generator/datadictionary/info/${22}`),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.optionsContractStatus = data && data.code === 0 ? data.list : [];
        console.log(data.list)
      });

      this.dataForm.contractId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.contractId) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/contractinfo/info/${this.dataForm.contractId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.stakeholder = data.contractInfo.stakeholder;
              this.dataForm.contractNum = data.contractInfo.contractNum;
              this.dataForm.contractType = data.contractInfo.contractType;
              this.dataForm.contractName = data.contractInfo.contractName;
              if (iStatus === 0) {
                this.dataForm.startTime = data.contractInfo.startTime;
              } else {
                this.dataForm.startTime = "";
              }
              if (iStatus === 0) {
                this.dataForm.endTime = data.contractInfo.endTime;
              } else {
                this.dataForm.endTime = "";
              }
              // dataForm.startTime !=='' ? '修改' : '续约'
              if(this.dataForm.startTime !=='') {
                this.dataForm.contractStatus = data.contractInfo.contractStatus;
              } else {
                this.dataForm.contractStatus = '';
              }
              
              this.dataForm.partyA = data.contractInfo.partyA;
              this.dataForm.partyB = data.contractInfo.partyB;
              this.dataForm.signData = data.contractInfo.signData;
              this.dataForm.createUserId = data.contractInfo.createUserId;
              this.dataForm.affiliatedCompanyId =
                data.contractInfo.affiliatedCompanyId;
              this.dataForm.createTime = data.contractInfo.createTime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/contractinfo/${this.info !== 0 ? 'save' : 'update'}`
            ),
            method: "post",
            data: this.$http.adornData({
              contractId: this.dataForm.contractId || undefined,
              stakeholder: this.dataForm.stakeholder,
              contractNum: this.dataForm.contractNum,
              contractType: this.dataForm.contractType,
              contractName: this.dataForm.contractName,
              startTime: this.dataForm.startTime,
              endTime: this.dataForm.endTime,
              contractStatus: this.dataForm.contractStatus,
              partyA: this.dataForm.partyA,
              partyB: this.dataForm.partyB,
              signData: this.dataForm.signData,
              createUserId: this.dataForm.createUserId,
              affiliatedCompanyId: this.dataForm.affiliatedCompanyId,
              createTime: this.dataForm.createTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
